
// <copyright file="DataTableToDataReaderAdapter.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The data table to data reader adapter.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data
{
    using System.Data;

    using SubhadraSolutions.Sharp.Utils.Data.Common;

    /// <summary>
    /// The data table to data reader adapter.
    /// </summary>
    public sealed class DataTableToDataReaderAdapter : AbstractDataReader
    {
        #region Fields

        /// <summary>
        /// The table.
        /// </summary>
        private readonly DataTable _table;

        /// <summary>
        /// The row index.
        /// </summary>
        private int _rowIndex = -1;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="DataTableToDataReaderAdapter"/> class.
        /// </summary>
        /// <param name="table">
        /// The table.
        /// </param>
        public DataTableToDataReaderAdapter(DataTable table)
        {
            this._table = table;
            this.ColumnNames.Clear();
            for (int i = 0; i < table.Columns.Count; i++)
            {
                this.ColumnNames.Add(table.Columns[i].ColumnName);
            }
        }

        #endregion Constructors and Finalizers

        #region Public Indexers

        /// <summary>
        /// The this.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        public override object this[int i]
        {
            get
            {
                return this._table.Rows[this._rowIndex][i];
            }
        }

        #endregion Public Indexers

        #region Methods

        /// <summary>
        /// The read protected.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        protected override bool readProtected()
        {
            this._rowIndex++;
            return this._rowIndex < this._table.Rows.Count;
        }

        #endregion Methods
    }
}